package com.fg.cloud.factory.controller.factory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.FactoryProcessEntity;
import com.fg.cloud.manager.service.factory.FactoryProcessService;

/**
 * Description:流程管理
 * @author 陈银龙
 * @date 2017年8月10日下午4:34:21
 */
@Controller
@RequestMapping(value="/process")
public class FactoryProcessController extends BaseController{

	@Autowired
	private FactoryProcessService processService ; 
	
	/**
	 * Description:新增(不用,暂时保留)
	 * @param req
	 * @param resp
	 * @param processEntity
	 * @author 陈
	 * @date 2017年8月10日下午4:34:47
	 */
	//@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert(HttpServletRequest req , HttpServletResponse resp , FactoryProcessEntity processEntity){
		try{
			processEntity.setCreator(getCurrentFactoryUser().getId());
			processEntity.setFacId(getCurrentFactoryUser().getFacId());
			if(processEntity == null || processEntity.getFacId() == null || processEntity.getFacId() < 1){
				doResult(resp, false, StatusCode.LACKVALUE, null, ResultType.INPUT_ERROR);
			}else{
				int flag = processService.insert(processEntity);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.ADD_FAULT);
				}
			}
		}catch(Exception e){
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	
	/**
	 * @Description: 修改流程节点 
	 * @param req
	 * @param resp
	 * @param processEntity 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月15日上午9:32:10
	 * @throws 
	 *
	 */
	@RequestMapping(value="/update",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void update(HttpServletRequest req , HttpServletResponse resp , FactoryProcessEntity processEntity){
		try{
			if(processEntity == null || processEntity.getId() == null || processEntity.getId() < 1){
				doResult(resp, false, StatusCode.LACKVALUE, null, ResultType.INPUT_ERROR);
			}else{
				processEntity.setFacId(getCurrentFactoryUser().getFacId());
				processEntity.setUpdater(getCurrentFactoryUser().getId());
				int flag = processService.update(processEntity);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
				}
			}
		}catch(Exception e){
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 通过厂商ID查询厂商流程节点信息 
	 * @param facId 厂商ID
	 * @author liushengchao
	 * @date 2017年6月15日下午3:55:58
	 * @throws 
	 *
	 */
	@RequestMapping(value="/selectbyfacid",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectByFacId(HttpServletRequest req , HttpServletResponse resp){
		try {
			Long facId = getCurrentFactoryUser().getFacId();
			if(facId == null || facId< 0){
				doResult(resp, true, StatusCode.FINDERROR, null, ResultType.INPUT_ERROR);
			}
			FactoryProcessEntity processEntity = processService.selectByFacId(facId);
			
			if (processEntity == null) {
				FactoryProcessEntity addprocessEntity = new FactoryProcessEntity();
				addprocessEntity.setCreator(getCurrentFactoryUser().getId());
				addprocessEntity.setFacId(getCurrentFactoryUser().getFacId());
				processService.insert(addprocessEntity);
				processEntity = processService.selectByFacId(facId);
				
				doResult(resp, true, StatusCode.FINDSUCCESS, processEntity, ResultType.FIND_SUCCESS);
			} else {
				doResult(resp, true, StatusCode.FINDSUCCESS, processEntity, ResultType.FIND_SUCCESS);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
}
